<template>
  <div class="mall page" ref="webview">
    <!-- <div class="nav-bar">
      <van-nav-bar class="bg_white" title="商城" />
    </div> -->
    <!-- <div class="mall-tab-bar">
      <template v-for="(item,index) in tabArr">
        <span class="tab" :key="index" :class="{active: activeIndex === item.id}" @click="changeTab(item.id)">{{item.name}}</span>
      </template>
    </div> -->
    <one-search></one-search>
    <!-- 轮播图s -->
    <div class="carousel_div">
      <wt-carousel :options="carousel_option">
        <div class="carousel-item" v-for="(item,index) of swiperList" :key="index"><a :href="item.ad_url"><img alt="item.ad_name" :src="item.ad_file"></a></div>
      </wt-carousel>
    </div>
    <!-- 轮播图e -->

    <div class="content">
      <div class="sidebar">
        <van-sidebar v-model="activeKey" @change="onChangeSidebar">
          <van-sidebar-item v-for="(item,index) in cateListName" :key="index" :title="item.name" />
        </van-sidebar>
      </div>
      <div class="classType">
        <div class="nodata" v-if="cateList.length === 0">暂无数据</div>
        <template v-for="(item,index) in cateList">
          <div class="rows" @click="goClassList(item)" :key="index">
            <div class="row-img">
              <van-image lazy-load :src="item.cate_img">
                <template v-slot:loading>
                  <van-loading type="spinner" size="24" />
                </template>
              </van-image>
            </div>
            <div class="name">{{item.name}}</div>
          </div>
        </template>
        <div class="rows"></div>
      </div>
    </div>
    <tab-bar v-model="isActive"></tab-bar>
  </div>
</template>
<script>
export default {
  name: 'mall',
  data () {
    return {
      carousel_option: {
        auto: true,
        loop: true,
        pagination: false,
        time: 3000
      },
      isActive: 3,
      activeIndex: 1,
      activeKey: 0,
      cateList: [],
      tabArr: [{
        name: '配送专区',
        id: 1
      }, {
        name: '复消专区',
        id: 2
      }],
      cateListName: [],
      swiperList: [],
      code: 'category'
    };
  },
  components: {
  },
  // 缓存了页面使用 created钩子
  created () {
    this.getCateDatelist(0, (pid) => {
      pid && this.getCateDatelist(pid);
    });
    this.getSwiperList();
  },
  methods: {
    onChangeSidebar (index) {
      this.getCateDatelist(this.cateListName[index].id);
    },
    changeTab (index) {
      this.activeIndex = index;
      this.getCateDatelist(0, (pid) => {
        pid && this.getCateDatelist(pid);
      });
    },
    goClassList (data) {
      sessionStorage.setItem('title', data.name);
      this.$router.push({ 'path': '/mall/list', query: { cid: data.id, area_id: this.activeIndex } });
    },
    getCateDatelist (pid, callbark) {
      this.$API.global.catelist({ pid: pid, cateArea: this.activeIndex }).then(res => {
        if (!pid) {
          this.cateListName = res.data;
        } else {
          this.cateList = res.data;
        }
        callbark && callbark(res.data && res.data[0] && res.data[0].id);
      });
    },
    getSwiperList() {
      let that = this;
      let options = {};
      options.code = that.code;
      this.$API.home.swiperList(options).then(res => {
        if (res.code === 0) {
          console.log(res.data);
          that.swiperList = res.data;
        }
        this.isLoading = false;
      });
    }
  }
};
</script>
<style lang="less" scoped>
.mall {
  ::-webkit-scrollbar {
    width: 0 !important;
    height: 0;
  }
  background: #fff;
  display: flex;
  flex-direction: column;
  .carousel_div{
    padding:20px 30px;
    border-top: 2px solid #bfbfbf;
    height:5rem;
  }
  .search{
    width:100%;
    height:1.1rem;
    background: #FAFAFA;
    .search_div{
      width:90%;
      margin:10px auto;
      background: #EEE;
      height:60px;
      line-height: 60px;
      border-radius: 30px;
      position: relative;
      padding-left:60px;
      box-sizing: border-box;
      color:#514C4C;
      font-size: 24px;
      img {
        position: absolute;
        left:20px;
        width:30px;
        top:50%;
        transform: translate(0,-50%);
      }
    }
  }
  .mall-tab-bar {
    // height: 56px;
    position: relative;
    margin-top: -1px; /*no*/
    z-index: 9;
    padding: 10px 0 30px;
    display: flex;
    justify-content: center;
    background-color: #fff;
    border-bottom: 1px solid #e8e9eb; /*no*/
    .tab {
      padding: 12px 16px;
      // height: 56px;
      border: 1px solid #007aff; /*no*/
      margin-left: -1px; /*no*/
    }
    .tab:first-child {
      border-top-left-radius: 8px;
      border-bottom-left-radius: 8px;
    }
    .tab:last-child {
      border-top-right-radius: 8px;
      border-bottom-right-radius: 8px;
    }
    .tab.active {
      background-color: #007aff;
      color: #fff;
    }
  }
  .content {
    width: 100%;
    position: relative;
    overflow-x: hidden;
    overflow-y: auto;
    background-color: #fff;
    height:calc(100vh - 7.4rem);
  }
  ::first .TabBar {
    flex: 0 0 110px;
    margin-top: 0;
  }
  .sidebar {
    width: 22%;
    background-color: #f5f5f5;
    margin-right: 0;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    overflow-x: hidden;
    overflow-y: auto;
    z-index: 99;
    border-right: 4px solid #BFBFBF;
  }
  .classType {
    background-color: #fff;
    margin-left: 22%;
    margin-right: 20px;
    padding: 20px 0 20px 20px;
    display: flex;
    flex-wrap: wrap;
    position: relative;
    justify-content: space-between;
    border-right:none;
    border-bottom:none;
    .nodata {
      position: absolute;
      left: 0;
      right: 0;
      text-align: center;
      height: 100%;
    }
    .rows {
      width:32%;
      .row-img {
        width: 100%;
        background-color: #eee;
        img {
          width: 210px;
          height: auto;
        }
      }
      .name {
        text-align: center;
        font-size: 24px;
        padding: 15px 0;
        color: #272525;
      }
    }
  }
  .van-sidebar {
    width: 100%;
    .van-sidebar-item {
      text-align: center;
      position: relative;
      line-height: 45px;
      color:#272525;
      &::before {
        content: '';
        display: block;
        position: absolute;
        top: 50%;
        transform:translate(0,-50%);
        left: 0;
        width: 5px;
        height: 50px;
        background-color: #fff;
      }
    }
    .van-sidebar-item.van-sidebar-item--select {
      border: none;
      color: #AF2B2B;
      font-weight: bold;
      background-color: #fff;
      &::before {
        content: '';
        display: block;
        position: absolute;
        top: 50%;
        transform:translate(0,-50%);
        left: 0;
        width: 3px;
        height: 50px;
        background-color: #AF2B2B;
      }
    }
  }
}
</style>
